import requests
import threading
import re


def save_img(url, img_name):
    with open(img_name, 'wb') as fp:
        fp.write(requests.get(img_url).content)
        print(f"{img_name}保存成功!")


# 获取首页html
host="https://www.sanchuangedu.cn/"
response = requests.get(url=host)
response.encoding = 'utf8'
html = response.text

# 正则
img_pattern = re.compile(r"<img.*?>")
img_url_pattern = re.compile(r"src=(\w*.(png|jpg))")

# 获取图片
img_lst = img_pattern.findall(html)
print(img_lst)
t_lst = []
for img in img_lst:
    img_name = img_url_pattern.search(img).group(1)
    img_url = host+img_name
    t = threading.Thread(target=save_img, args=(img_url, img_name))
    t.start()
    t_lst.append(t)
for t in t_lst:
    t.join()